<template>
  <!-- :destroy-on-close="true": 销毁弹框,用于清空误操作产生的校验提示 -->
  <el-dialog
    :title="formData.id?'编辑经纪':'添加经纪'"
    :visible="isShowDialog"
    :destroy-on-close="true"
    @close="btnCancel"
  >
    <el-form ref="addForm" label-width="120px" :model="formData" label-position="top" :rules="rules">
      <el-form-item label="名称" prop="name">
        <el-input v-model="formData.name" placeholder="请输入名称" />
      </el-form-item>
      <el-form-item label="邮箱" prop="email">
        <el-input v-model="formData.email" placeholder="请输入邮箱" />
      </el-form-item>
      <el-form-item label="电话" prop="phone">
        <el-input v-model="formData.phone" placeholder="请输入电话" />
      </el-form-item>
      <el-form-item label="地址" prop="address">
        <el-input v-model="formData.address" placeholder="请输入地址" />
      </el-form-item>
      <el-form-item label="描述" prop="desc" value="good">
        <el-input
          v-model="formData.desc"
          type="textarea"
          :autosize="{ minRows: 2, maxRows: 4}"
          placeholder="请输入描述"
        />
      </el-form-item>
      <el-form-item label="封面" prop="image">
        <ImageUpload ref="fileList" v-model="formData.image" />
      </el-form-item>
    </el-form>

    <template #footer>
      <el-row type="flex" justify="center">
        <el-button size="small" type="primary" @click="btnOk">确定</el-button>
        <el-button size="small" @click="btnCancel">取消</el-button>
      </el-row>
    </template>
  </el-dialog>
</template>

<script>
import { addagents, editAgents } from '@/api/agent'
import ImageUpload from '@/components/ImageUpload'
export default {
  components: { ImageUpload },
  // 父传子 子接收
  props: {
    isShowDialog: {
      type: Boolean,
      default: false
    }
  },
  data() {
    return {
      formData: {
        name: '', // 名称
        email: '', // 邮箱
        phone: '', // 电话
        desc: '', // 描述
        address: '', // 地址
        image: '', // 封面
        id: ''
      },
      rules: {
        name: [
          { required: true, message: '请输入活动名称', trigger: 'blur' },
          { min: 2, max: 10, message: '长度在 2 到 4 个字符', trigger: 'blur' }
        ],
        email: [
          { required: true, message: '请输入邮箱地址', trigger: 'blur' }
          // { pattern: '/^(\w)+(\.\w+)*@(\w)+((\.\w{2,3}){1,3})$/', message: '请输入正确的邮箱', trigger: 'blur' }
        ],
        phone: [{ required: true, message: '手机号不能为空', trigger: 'blur' },
          { pattern: /^1[3-9]\d{9}$/, message: '手机号格式不对', trigger: 'blur' }
        ],
        desc: [
          { required: true, message: '请输入描述', trigger: 'blur' },
          { min: 2, max: 200, message: '长度在 2 到 200 个字符', trigger: 'blur' }
        ],
        address: [
          { required: true, message: '请输入地址', trigger: 'blur' }
        ]
      }
    }
  },

  methods: {
    // 确定按钮
    async btnOk() {
      try {
        // 校验表单fileList
        await this.$refs.addForm.validate()
        // 发送请求
        // 封面图处理
        const fileList = this.$refs.fileList.fileList
        // if (fileList[0] && fileList[0].url.indexOf('blob') !== -1) {
        //   fileList[0].url = fileList[0].url.slice(5)
        // }
        if (fileList[0] && fileList[0].status !== 'success') {
          this.$message.warning('请等待图片上传')
          return
        }
        // 判断有没有图片
        if (fileList.length === 0) {
          this.$message.warning('请上传图片')
          return
        }
        if (this.formData.id) {
        // 编辑请求接口 保存新数据到服务器
          await editAgents({ ...this.formData, image: fileList[0] ? fileList[0].url : '' })
        } else {
        // 新增请求接口 保存新数据到服务器
          await addagents({ ...this.formData, image: fileList[0] ? fileList[0].url : 'http/' })
        }
        // 提示用户
        this.formData.id ? this.$message.success('编辑成功') : this.$message.success('添加成功')
        // 关闭弹窗
        this.$emit('update:isShowDialog', false)
        // 更新页面, 子传父
        this.$emit('addAgent')
      } catch (err) {
        console.log(err)
      }
    },
    // 取消按钮
    btnCancel() {
      // 1. 清空表单数据
      this.formData = {}
      // 2. 清空表单校验
      this.$refs.addForm.resetFields()
      // 清空可能存在的图片
      this.$refs.fileList.fileList = []
      // 3 关闭弹窗
      this.$emit('update:isShowDialog', false)
    }
  }
}
</script>

<style>

</style>
